package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.AddressDao;
import org.example.Pojo.Address;
import org.example.Service.AddressService;
import org.example.Utils.UnionResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressDao addressDao;
    @Override
    public UnionResult addAddress(Address address) {
        if (addressDao.insert(address)>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult updateAddress(Address address) {
        if (addressDao.updateById(address) > 0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult deleteAddress(Address address) {
        if (addressDao.delete(new QueryWrapper<Address>().eq("uid",address.getUid()))>0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult getAddress(Address address) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>();
        queryWrapper.eq("uid",address.getUid());
        return UnionResult.Success("",addressDao.selectOne(queryWrapper));
    }

    @Override
    public UnionResult getAllMyAddress(String userid) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        return UnionResult.Success("",addressDao.selectList(queryWrapper));
    }
}
